/**
 * 通过递归方式实现a->b->c->d-e->f 链式反转 f->e-d->c-b-a
 */
package com.zhuiwei.exercise;

    public class NodeTest {

        public static class Node {
            public String value;
            public Node next;

            public Node(String data) {
                this.value = data;
            }
        }

        /**
         * 打印输出
         */
        public static void print(Node node){
            while (node!=null){
                System.out.print(node.value);
                node=node.next;
                if (node!=null){
                    System.out.print("->");
                }else {
                    System.out.println();
                }
            }
        }

        /**
         * 递归方法
         */
        public static Node reverse(Node head) {
            if (head == null || head.next == null)
                return head;
            Node newHead = reverse(head.next);//最后一个进入head.next为f，head就是e
            head.next.next = head;//最后一个进入f 指向e  head.next.next=head
            head.next = null;
            return newHead;
        }


        public static void main(String[] args) {
            Node a = new Node("a");
            Node b = new Node("b");
            Node c = new Node("c");
            Node d = new Node("d");
            Node e = new Node("e");
            Node f = new Node("f");
            a.next = b;
            b.next = c;
            c.next = d;
            d.next = e;
            e.next = f;
//            print(a);
            print(reverse(a));
        }
    }


